package com.ict.ms.controller;

import com.ict.common.StateMsg;
import com.ict.redis.pojo.Fuse;
import com.ict.redis.redisService.TrackFuseService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by chenlong on 2016/11/15.
 */

/**
 * 单船态势回放
 * 单船航迹控制
 */

@Controller
@RequestMapping({"/track"})
public class TrackFuseController {
    @Resource
    TrackFuseService trackFuseService2;
//    @Resource
//    OriginalService originalService2;

    /**
     * 单船态势回放
     * @param map
     * @param modeType
     * @param shipId
     * @param startTime
     * @param endTime
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "oneship.do",method = RequestMethod.POST)
    public String TrackFuse(ModelMap map, @RequestParam(value="modeType", required = false) int modeType,
                            @RequestParam(value="shipId", required = false) int shipId,
                           @RequestParam(value="startTime", required = false) int startTime,
                           @RequestParam(value="endTime", required = false) int endTime){

        String msg;
        String jsonStr;
        String md;
        int minTime=Integer.MAX_VALUE;
        int maxTime=Integer.MIN_VALUE;



//        融合后目标的key
        if(modeType==0){
            md="MDV"+shipId;

            List<Fuse> trackFuseList=trackFuseService2.getFuse_original(md,startTime,endTime);

            if(trackFuseList.size()==0){
                msg="获取指定目标信息失败";
                map.addAttribute("error",msg);
                jsonStr = StateMsg.toJson(-1, map);
                return jsonStr;
            }
            if(minTime>trackFuseList.get(0).getTi()){
                minTime=trackFuseList.get(0).getTi();
            }
            if(maxTime<trackFuseList.get(trackFuseList.size()-1).getTi()){
                maxTime=trackFuseList.get(trackFuseList.size()-1).getTi();
            }

            map.addAttribute("minTime",minTime);
            map.addAttribute("maxTime",maxTime);
            map.addAttribute("num",shipId);
            map.addAttribute("mode",0);
            map.addAttribute("posList",trackFuseList);
        }else{
            md="ODV"+shipId+"_"+modeType;
            List<Fuse> trackFuseList=trackFuseService2.getFuse_original(md,startTime,endTime);
            if(trackFuseList.size()==0){
                msg="获取指定目标信息失败";
                map.addAttribute("error",msg);
                jsonStr = StateMsg.toJson(-1, map);
                return jsonStr;
            }
            if(minTime>trackFuseList.get(0).getTi()){
                minTime=trackFuseList.get(0).getTi();
            }
            if(maxTime<trackFuseList.get(trackFuseList.size()-1).getTi()){
                maxTime=trackFuseList.get(trackFuseList.size()-1).getTi();
            }

            map.addAttribute("minTime",minTime);
            map.addAttribute("maxTime",maxTime);
            map.addAttribute("num",shipId);
            map.addAttribute("mode",modeType);
            map.addAttribute("posList",trackFuseList);
        }

        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }

    /**
     * 单船航迹控制
     * @param map
     * @param modeType
     * @param shipId
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "limitpoint.do",method = RequestMethod.POST)
    public String TrackFuseLimit(ModelMap map, @RequestParam(value="modeType", required = false) int modeType,
                            @RequestParam(value="shipId", required = false) int shipId,
                            @RequestParam(value="limit", required = false) int limit){

        String msg;
        String jsonStr;
        String md;

//        融合后目标的key
        if(modeType==0){
            md="MDV"+shipId;

            List<Fuse> trackFuseList=trackFuseService2.getFuse_original_Track(md,limit);
            if(trackFuseList.size()==0){
                msg="获取指定目标信息失败";
                map.addAttribute("error",msg);
                jsonStr = StateMsg.toJson(-1, map);
                return jsonStr;
            }

            map.addAttribute("posList",trackFuseList);
        }else{
            md="ODV"+shipId+"_"+modeType;
            List<Fuse> trackFuseList=trackFuseService2.getFuse_original_Track(md,limit);
            if(trackFuseList.size()==0){
                msg="获取指定目标信息失败";
                map.addAttribute("error",msg);
                jsonStr = StateMsg.toJson(-1, map);
                return jsonStr;
            }

            map.addAttribute("num",shipId);
            map.addAttribute("mode",modeType);
            map.addAttribute("posList",trackFuseList);
        }

        jsonStr = StateMsg.toJson(1, map);
        return jsonStr;
    }
}